<template>
  <div class="energy_machine_g">
    <c-title :hide="false" text="设备商品页"></c-title>
    <div class="energy_machine_Good">
      <div class="lis" v-for="(item, index) in list" :key="index">
        <div class="left">
          <img :src="item.thumb" alt="" />
        </div>
        <div class="right">
          <div class="title">{{ item.title }}</div>
          <div class="time" v-if="item.has_one_plugin_goods">时长：{{ item.has_one_plugin_goods.run_time }}秒</div>
          <div class="buy">
            <div class="price">¥{{ item.price }}</div>
            <div class="addimg" @click="toBuy(item.plugin_id, item)">购买</div>
          </div>
        </div>
      </div>
    </div>
    <div class="btnBox">
      <div class="btn" v-if="buttons.button1" @click="toUrl(buttons.button1_url)">{{ buttons.button1 }}</div>
      <div class="btn" v-if="buttons.button2" @click="toUrl(buttons.button2_url)">{{ buttons.button2 }}</div>
    </div>
    
  </div>
</template>
<script>
import { scrollMixin } from "utils/mixin";
export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      list: [],
      buttons: [],

      isLoadMore: true,
      page: 1,
      total_page: 0
    };
  },
  activated() {
    console.log(this.$route.params);
    this.getData();
    this.bottomButton();
  },
  methods: {
    toUrl(url) {
      if (!url) return;
      window.location.href = url;
    },
    bottomButton() {
      let json = {};
      json.energy_machine_id = this.$route.params.machine_id;
      $http.get("plugin.energy-machine.frontend.goods.bottomButton", json, "loading").then(response => {
        if (response.result == 1) {
          console.log(response.data);
          this.buttons = response.data;
        } else {
          this.$toast(response.msg);
        }
      });
    },
    toBuy(plugin_id, item) {
      if (plugin_id == 32) {
        if (item.has_one_store_goods) {
          this.$router.push(
            this.fun.getUrl(
              "goodsorder",
              {},
              {
                store_id: item.has_one_store_goods.store_id,
                tag: "store",
                energy_machine_id: this.$route.params.machine_id,
                goodsId: item.goods_id,
                runningTime:item.has_one_plugin_goods.run_time
              }
            )
          );
        }
      } else {
        this.$router.push(
          this.fun.getUrl(
            "goodsorder",
            {},
            {
              tag: "-2",
              energy_machine_id: this.$route.params.machine_id,
              goodsId: item.goods_id,
              total: 1,
              runningTime:item.has_one_plugin_goods.run_time
            }
          )
        );
      }
    },
    getData() {
      let json = {};
      json["search[energy_machine_id]"] = this.$route.params.machine_id;
      $http.get("plugin.energy-machine.frontend.goods.searchGoods", json, "loading").then(response => {
        if (response.result == 1) {
          let _data = response.data;
          this.list = _data.data;
          this.isLoadMore = true;
          this.total_page = _data.last_page;
          if (!this.total_page) {
            this.total_page = 0;
          }
        } else {
          this.$toast(response.msg);
        }
      });
    },
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        let urlstr = "plugin.energy-machine.frontend.goods.searchGoods";
        let json = {
          page: this.page
        };
        json["search[energy_machine_id]"] = this.$route.params.machine_id;
        $http.get(urlstr, json, "加载中").then(
          response => {
            this.isLoadMore = true;
            if (response.result === 1) {
              var myData = response.data.data;
              this.list = this.list.concat(myData); //数组拼接
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
              return;
            }
          },
          function(response) {
            // error callback
          }
        );
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.energy_machine_g{
  padding-bottom: 3.1rem;
}
.btnBox {
  position: fixed;
  width: 100%;
  height: 3.06rem;
  left: 0;
  bottom: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #fff;
  .btn {
    flex: 1;
  }
}
.energy_machine_Good {
  margin: 0.63rem 0.75rem;
  background: #fff;
  border-radius: 0.5rem;
  padding: 0 0.75rem;
  .lis {
    padding: 0.75rem 0 0.63rem 0;
    border-bottom: 1px solid #f0f0f1;
    display: flex;
    .left {
      width: 5.63rem;
      height: 5.63rem;
      border-radius: 0.5rem;
      margin-right: 0.63rem;
      flex-shrink: 0;
      img {
        width: 100%;
        height: 100%;
        border-radius: 0.5rem;
      }
    }
    .right {
      display: flex;
      flex-direction: column;
      flex: 1;
      .title {
        font-weight: 500;
        line-height: 1.13rem;
        font-size: 0.88rem;
        text-align: left;
      }
      .time {
        font-weight: 400;
        color: #6e6e79;
        font-size: 0.81rem;
        margin: 0.63rem 0 0.63rem 0;
        text-align: left;
      }
      .buy {
        height: 2rem;
        border-radius: 1rem;
        background: #fff2f2;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-left: 0.63rem;
        .price {
          font-size: 0.88rem;
          font-weight: bold;
          line-height: 1.13rem;
          color: #f15353;
        }
        .addimg {
          width: 4.06rem;
          height: 2rem;
          background-image: url("https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/energy_machine%2Fadd.png");
          background-size: 100% 100%;
          background-repeat: no-repeat;
          display: flex;
          align-items: center;
          justify-content: center;
          color: #fff;
        }
      }
    }
  }
}
</style>
